package com.example.service.impl.pc;

import com.alibaba.fastjson2.JSONArray;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.config.RedisCache;
import com.example.entity.dto.pc.ResourceGroupListDto;
import com.example.entity.mysql.Resourcegroup;
import com.example.entity.mysql.Workresource;
import com.example.entity.vo.pc.LoadTimeVo;
import com.example.entity.vo.pc.ResourceGroupLoadVo;
import com.example.entity.vo.pc.ResourceGroupVo;
import com.example.entity.yifei.Mocty;
import com.example.mapper.MoctyMapper;
import com.example.mapper.pc.ResourcegroupMapper;
import com.example.mapper.pc.WorkresourceMapper;
import com.example.service.pc.ResourcegroupService;
import com.example.untils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiangPandeng
 * @since 2024-05-19
 */
@Service
public class ResourcegroupServiceImpl extends ServiceImpl<ResourcegroupMapper, Resourcegroup> implements ResourcegroupService {

    @Autowired
    private ResourcegroupMapper resourcegroupMapper;

    @Resource
    private WorkresourceMapper workresourceMapper;
    @Autowired
    private ResourcegroupService resourcegroupService;
    @Resource
    private MoctyMapper moctyMapper;
    @Autowired
    private RedisCache redisCache;

    @Override
    public List<Resourcegroup> getResourceGroupListFromRedis() {
        List<Resourcegroup> resourceGroupList = redisCache.getCacheList("resourceGroupList");
        if(resourceGroupList == null || resourceGroupList.size() == 0) {
            resourceGroupList = resourcegroupService.list();
            redisCache.setCacheList("resourceGroupList", resourceGroupList);
        }
        JSONArray arrayCache = redisCache.getCacheObject("resourceGroupList");
        if (arrayCache != null)
        {
            return arrayCache.toList(Resourcegroup.class);
        }
        return null;
    }

    @Override
    public List<ResourceGroupVo> resourceGroupList(ResourceGroupListDto dto) {
        return resourcegroupMapper.resourceGroupList(dto);
    }

    @Override
    public Integer getOriginIdById(Integer id) {
        // 校验该资源组是否属于该资源类别
        List<Resourcegroup> list = resourcegroupMapper.selectList(null);
        Map<Integer, Resourcegroup> posMap = new HashMap<>();
        for(Resourcegroup t : list) {
            posMap.put(t.getResourceGroupId(), t);
        }
        return getOriginIdById(id, posMap);
    }

    @Override
    public Integer getOriginIdById(Integer id, Map<Integer, Resourcegroup> posMap) {
        Resourcegroup resourcegroup = posMap.get(id);
        if(resourcegroup.getResourceGroupParentId() == null){
            return resourcegroup.getResourceGroupId();
        }
        return getOriginIdById(resourcegroup.getResourceGroupParentId(), posMap);
    }

    /**
     * 查看资源组负荷
     * @param id 资源组id
     * @param l  日期区间
     * @param r
     * @return
     */
    @Override
    public List<ResourceGroupLoadVo> getResourceGroupLoad(Integer id, String l, String r) {
        if(id == null || id == 0) {
            return null;
        }
        Map<String, BigDecimal> map = new HashMap<>();
        List<ResourceGroupLoadVo> res = new ArrayList<>();
        LambdaQueryWrapper<Workresource> queryWrapper = new LambdaQueryWrapper<Workresource>().eq(Workresource::getResourceGroupId, id);
        if(l != null) queryWrapper.ge(Workresource::getDate, l);
        if(r != null) queryWrapper.le(Workresource::getDate, r);
        List<Workresource> list = workresourceMapper.selectList(queryWrapper);
        for(Workresource workresource : list) {
            BigDecimal bigDecimal = map.get(workresource.getDate());
            if(bigDecimal == null) bigDecimal = new BigDecimal(0);

            String productLineId = workresource.getProductLineId();
            Mocty mocty = moctyMapper.selectOne(new LambdaQueryWrapper<Mocty>().eq(Mocty::getUdf05, productLineId));
            if(workresource.getBeat() != null && workresource.getBeat() > 0) {
                Double v = new Double(mocty.getTy004() * workresource.getBeat() / 3600);
                bigDecimal.add(new BigDecimal(v));
            }else{
                if(workresource.getQuota() != null && workresource.getQuota() > 0) {
                    Double v = new Double(mocty.getTy004() / workresource.getQuota());
                    bigDecimal.add(new BigDecimal(v));
                }
            }
            map.put(workresource.getDate(), bigDecimal);
        }
        map.forEach((key, value) -> {
//            System.out.println("Key: " + key + ", Value: " + value);
            res.add(new ResourceGroupLoadVo(key, value, null));
        });
        res.sort(new Comparator<ResourceGroupLoadVo>() {
            @Override
            public int compare(ResourceGroupLoadVo o1, ResourceGroupLoadVo o2) {
                return o1.getDate().compareTo(o2.getDate());
            }
        });
        return res;
    }

    /**
     * 查看资源组中具体资源负荷
     * @return
     */
    @Override
    public List<ResourceGroupLoadVo> getResourceLoad(Integer id) {
        if(id == null || id == 0) {
            return null;
        }
        Map<String, BigDecimal> map = new HashMap<>();
        List<ResourceGroupLoadVo> res = new ArrayList<>();
        List<Workresource> list = workresourceMapper.selectList(new LambdaQueryWrapper<Workresource>().eq(Workresource::getResourceId, id));
        for(Workresource workresource : list) {
            BigDecimal bigDecimal = map.get(workresource.getDate());
            if(bigDecimal == null) bigDecimal = new BigDecimal(0);

            String productLineId = workresource.getProductLineId();
            Mocty mocty = moctyMapper.selectOne(new LambdaQueryWrapper<Mocty>().eq(Mocty::getUdf05, productLineId));
            if(workresource.getBeat() != null && workresource.getBeat() > 0) {
                Double v = new Double(mocty.getTy004() * workresource.getBeat() / 3600);
                bigDecimal.add(new BigDecimal(v));
            }else{
                if(workresource.getQuota() != null && workresource.getQuota() > 0) {
                    Double v = new Double(mocty.getTy004() / workresource.getQuota());
                    bigDecimal.add(new BigDecimal(v));
                }
            }
            map.put(workresource.getDate(), bigDecimal);
        }
        map.forEach((key, value) -> {
//            System.out.println("Key: " + key + ", Value: " + value);
            res.add(new ResourceGroupLoadVo(key, value, null));
        });
        res.sort(new Comparator<ResourceGroupLoadVo>() {
            @Override
            public int compare(ResourceGroupLoadVo o1, ResourceGroupLoadVo o2) {
                return o1.getDate().compareTo(o2.getDate());
            }
        });
        return res;
    }

    @Override
    public List<LoadTimeVo> getResourceGroupLoadList() {

//        resourcegroupMapper.

        return null;
    }
}
